package com.gckj.dfds.backup;

import com.alibaba.fastjson.JSONArray;
import com.zhss.rpc.service.NameNodeServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

/**
 * @description:
 * @Author liu Weidong
 * @Email:
 * @Date: 2024/5/6 9:37
 * @Version 1.0
 */
public class NameNodeClientRpc {

    private final ManagedChannel channel;
    private final NameNodeServiceGrpc.NameNodeServiceBlockingStub blockingStub;
    public static final String IP = "localhost";
    public static final Integer PORT = 50072;
    // 构造函数，初始化 gRPC 通道和服务存根
    public NameNodeClientRpc() {
        this.channel = ManagedChannelBuilder.forAddress(IP, PORT)
                .usePlaintext()
                .build();
        this.blockingStub = NameNodeServiceGrpc.newBlockingStub(channel);
    }

    /**
     * 拉取log日志
     * @return
     */
    public JSONArray fetchLog(Long syncTxid) {
        NameNodeModel.FetchLogRequest fetchLogRequest = NameNodeModel.FetchLogRequest.newBuilder().setSize(150).setSyncTxid(syncTxid).build();
        NameNodeModel.FetchLogResponse fetchLogResponse = blockingStub.fetchLog(fetchLogRequest);
        String data = fetchLogResponse.getData();
        return  JSONArray.parseArray(data);
    }

    /**
     *  更新checkPointertxid
     * @param fsImage
     */
    public void updateCheckPointerTxid(FsImage fsImage) {
        NameNodeModel.UpdateTxidRequest updateTxidRequest = NameNodeModel.UpdateTxidRequest.newBuilder().setTxid(fsImage.getTxid()).build();
        NameNodeModel.UpdateTxidResponse updateTxidResponse = blockingStub.updateCheckPonterTxid(updateTxidRequest);
        int status = updateTxidResponse.getStatus();
        System.out.println("更新txid成功。。。。。。。");
    }


}
